<?php
/**[字段管理控制器]
 * @Author: 976123967@qq.com
 * @Date:   2014-09-23 13:44:16
 * @Last Modified by:   chen
 * @Last Modified time: 2014-10-28 22:34:51
 */
class FieldAction extends PublicAction{


	public $getMid;
	public $postMid;

	/**
	 * [__auto 初始化]
	 * @return [type] [description]
	 */
	public function _initialize()
	{

		parent::_initialize();
		// 获取getmid
		if($getMid = I('get.mid'))
		{
			$this->getMid = $getMid;
			$modelName = D('Model')->get_field($getMid,'remark');
			$this->assign('modelName',$modelName);
		} 

		// 获取postmid
		if($postMid = I('post.mid'))
		{
			$this->postMid = $postMid;
		} 



		$this->model = D('Field');
	}



	/**
	 * [index 所有字段]
	 * @return [type] [description]
	 */
	public function index()
	{
		$mid = I('get.mid',null,'intval');
		$data = $this->model->get_field_all($mid);
		$this->assign('data',$data);
		$this->display();
	}

	/**
	 * [update_cache 更新缓存]
	 * @return [type] [description]
	 */
	public function update_cache()
	{
		$mid = I('get.mid');
		$this->model->update_cache();
		$this->success('字段缓存更新成功',U('Field/index',array('mid'=>$mid)));
	}


	/**
	 * [add 添加字段]
	 */
	public function add()
	{


		if(IS_POST)
		{
			if(!$this->model->create()) $this->error($this->model->getError());
			$this->model->add_field();

			$this->success('字段添加成功',U('Field/index',array('mid'=>$_POST['mid'])));
			die;
		}


		$this->display();
	}

	/**
	 * [edit 编辑字段]
	 * @return [type] [description]
	 */
	public function edit()
	{
		if(IS_POST)
		{
			if(!$this->model->create()) $this->error($this->model->getError());
			$this->model->edit_field();

			$this->success('字段编辑成功',U('Field/index',array('mid'=>$_POST['mid'])));
			die;
		}

		$fid = I('get.fid');
		$field = $this->model->find_one($fid);
		$field['field_value'] = D('FieldValue')->get_field_all($fid);

		$this->assign('field',$field);
		$this->display();
	}

	public function del()
	{
		$mid= I('get.mid');
		$fid = I('get.fid');
		$this->model->del_field($fid);
		$this->success('字段删除成功',U('Field/index',array('mid'=>$mid)));
	}

	/**
     * [update_order 排序]
     * @return [type] [description]
     */
    public function update_order()
    {
        if(IS_POST)
        {
           $mid = I('post.mid');
            // 验证和排序
           if(!$this->model->update_order())
                $this->error($this->model->getError(),U('Field/index',array('mid'=>$mid)));
            $this->success('字段排序成功',U('Field/index',array('mid'=>$mid)));
        }
    }
}